﻿using CommandProcessor.Command;
using Data.Infrastructure;
using Data.Models;
using Data.Repositories;
using Domain.Commands;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Domain.Handlers
{
    public class CreateOrUpdatePurchaseDetailHandler : ICommandHandler<CreateOrUpdatePurchaseDetailCommand>
    {
        private readonly IPurchaseDetailRepository purchaseDetailRepository;
        private readonly IUnitOfWork unitOfWork;
        public CreateOrUpdatePurchaseDetailHandler(IPurchaseDetailRepository purchaseDetailRepository, IUnitOfWork unitOfWork)
        {
            this.purchaseDetailRepository = purchaseDetailRepository;
            this.unitOfWork = unitOfWork;
        }
        public ICommandResult Execute(CreateOrUpdatePurchaseDetailCommand command)
        {
            var purchaseDetail = new PurchaseDetail
            {
                ID = command.ID,
                Description=command.Description,
                Discount=command.Discount,
                GoodID=command.GoodID,
                Price=command.Price,
                PurchaseID=command.PurchaseID,
                Quantity=command.Quantity,
                TotalMoney=command.TotalMoney,
            };
            if (purchaseDetail.ID == 0)
                purchaseDetailRepository.Add(purchaseDetail);
            else
                purchaseDetailRepository.Update(purchaseDetail);

            unitOfWork.Commit();
            return new CommandResult(true);
        }
        public ICommandResult ExecuteRange(IEnumerable<CreateOrUpdatePurchaseDetailCommand> commands)
        {
            throw new NotImplementedException();
        }
        public ICommandResult DeleteRange(IEnumerable<CreateOrUpdatePurchaseDetailCommand> commands)
        {
            throw new NotImplementedException();
        }
    }
}
